/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ICMSContTypePanel.java
 *
 * Created on May 14, 2011, 3:58:36 PM
 */
package com.tecnick.jxhtmledit;

import com.tecnick.htmlutils.htmlurls.HTMLURLs;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

/**
 *
 * @author Jay
 */
public class CMSContTypePanel extends javax.swing.JPanel {

    /** Creates new form ICMSContTypePanel */
    public CMSContTypePanel(String buttonspath) {
        initComponents();

//        ClassLoader cl = getClass().getClassLoader();
//        URL policyURL = cl.getResource("other/client.policy");
//        System.setProperty("java.security.policy", policyURL.toString());
//        System.setSecurityManager(new RMISecurityManager());
        

        ImageIcon img = getImageIcon(buttonspath + "awesome.jpg");
        jPanel1.setLayout(new GridLayout(0, 2));
        CMSDatabaseInterface idi = new CMSDatabaseInterface();
        ArrayList<String[]> tagpairs = null;
        try {
            tagpairs = idi.getContentTypeList();
        } catch (SQLException ex) {
            return;
        }
        for (int i = 0; i < tagpairs.size(); i++) {
            JLabel label = new JLabel(tagpairs.get(i)[0]);
            JLabel imglabel = new JLabel(img);
            String Payload = "<" + tagpairs.get(i)[1] + ">";
            //addDnDLabelListener(label);
            jPanel1.add(imglabel);
            jPanel1.add(label);
            jPanel1.revalidate();
            CMSJLabelDragSource dragSource = new CMSJLabelDragSource(label, Payload);
            CMSJLabelDragSource imgdragSource = new CMSJLabelDragSource(imglabel, Payload);
        }
    }

    public static void main(String[] args) {
        JFrame j = new JFrame();
        j.add(new CMSContTypePanel("null"));
        //j.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        j.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 486, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 416, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel1);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void addDnDLabelListener(JLabel label) {
        label.setTransferHandler(new TransferHandler("text"));
        MouseListener ml = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                JComponent jc = (JComponent) e.getSource();
                TransferHandler th = jc.getTransferHandler();
                th.exportAsDrag(jc, e, TransferHandler.COPY);
            }
        };
        label.addMouseListener(ml);
    }

    private ImageIcon getImageIcon(String imgpath) {
        ImageIcon img = null;
        try {
            InputStream in = getClass().getResourceAsStream(imgpath);
            byte buffer[];
            buffer = new byte[in.available()];
            in.read(buffer);
            img = new ImageIcon(buffer);
        } catch (Exception e1) {
            try {
                img = new ImageIcon(HTMLURLs.setURL(imgpath));
            } catch (Exception e2) {
                System.err.println(e2 + " :: " + imgpath);
            }
        }
        return img;
    }
}
